<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Sitevideoview
 * @copyright  Copyright 2011-2012 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: Controller.php 2012-06-028 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
class Sitevideoview_Widget_ListRecentVideosController extends Engine_Content_Widget_Abstract {

  public function indexAction() {
    // Should we consider creation or modified recent?
    $recentType = $this->_getParam('recentType', 'creation');
    if (!in_array($recentType, array('creation', 'modified'))) {
      $recentType = 'creation';
    }
    $this->view->recentType = $recentType;
    $this->view->recentCol = $recentCol = $recentType . '_date';
		$sitevideoview_recent_video = Zend_Registry::isRegistered('sitevideoview_recent_video') ? Zend_Registry::get('sitevideoview_recent_video') : null;

    // Get paginator
    $table = Engine_Api::_()->getItemTable('video');
    $select = $table->select()
            ->where('search = ?', 1)
            ->where('status = ?', 1);
    if ($recentType == 'creation') {
      // using primary should be much faster, so use that for creation
      $select->order('video_id DESC');
    } else {
      $select->order($recentCol . ' DESC');
    }
    $this->view->paginator = $paginator = Zend_Paginator::factory($select);
    // Set item count per page and current page number
    $paginator->setItemCountPerPage($this->_getParam('itemCountPerPage', 4));
    $paginator->setCurrentPageNumber($this->_getParam('page', 1));

    // Hide if nothing to show
    if (($paginator->getTotalItemCount() <= 0) || empty($sitevideoview_recent_video)) {
      return $this->setNoRender();
    }
  }

}